<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务名称" prop="productTitle">
        <el-input
          v-model="queryParams.productTitle"
          placeholder="请输入服务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="服务类型" prop="productType">
              <el-select v-model="queryParams.productType" placeholder="请选择服务类型" clearable size="small">
                <el-option
                  v-for="dict in productTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>-->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['web:product:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['web:product:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['web:product:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['web:product:export']"
            >导出
            </el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" size="mini" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="服务名称" align="center" prop="productTitle" width="180"/>
      <el-table-column label="服务类型" align="center" prop="productType" :formatter="productTypeFormat" width="120"/>
      <el-table-column label="国家地区" align="center" prop="productGuojia" :formatter="productGuojiaFormat" width="140"/>
      <el-table-column label="小程序图片" align="center" width="180">
        <template slot-scope="scope">
          <el-image
            style="width: 30px; height: 30px;margin-right: 8px;"
            :src="scope.row.productImg"
            :preview-src-list="scope.row.productImg">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="PC图片" align="center" width="180">
        <template slot-scope="scope">
          <el-image
            style="width: 30px; height: 30px;margin-right: 8px;"
            :src="scope.row.productImgpc"
            :preview-src-list="scope.row.productImgpc">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="patentPrice" width="100"/>
      <el-table-column label="其他" align="center" prop="otherPriceInfo" :formatter="otherPriceInfoFormat"/>
      <el-table-column label="总价" align="center" prop="patentTotalPrice" width="100"/>
      <el-table-column label="详情" align="center">
        <template slot-scope="scope">
          <!--          <el-image
                      v-for="item in scope.row.productDetailImg.split(',')"
                      style="width: 30px; height: 30px;margin-right: 8px;"
                      :src="item"
                      :preview-src-list="scope.row.productDetailImg.split(',')">
                    </el-image>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:product:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:product:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对象对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
      v-dialogDrag
      class="my_dialog">
      <el-form ref="form" size="mini" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务名称" prop="productTitle">
          <el-input v-model="form.productTitle" placeholder="请输入服务名称"/>
        </el-form-item>
        <el-form-item label="服务类型" prop="productType">
          <el-select v-model="form.productType" disabled placeholder="请选择服务类型" style="width: 100%;">
            <el-option
              v-for="dict in productTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="国家/地区" prop="productGuojia">
          <el-select v-model="form.productGuojia" placeholder="请选择国家/地区" style="width: 100%;">
            <el-option
              v-for="dict in yjlmGuojiaOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小程序图片" prop="productImg">
          <multiple-upload :max-count="1" list-type="picture-card" v-model="form.productImgList"/>
        </el-form-item>
        <el-form-item label="电脑端图片" prop="productImg">
          <multiple-upload :max-count="1" list-type="picture-card" v-model="form.productImgpcList"/>
        </el-form-item>

        <el-form-item label="价格" prop="patentPrice">
          <el-input v-model="form.patentPrice" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item v-for="(item,index) in this.form.otherPrice" :label="item.label">
          <el-input v-model="form.otherPrice[index].label" v-show="false"/>
          <el-input v-model="form.otherPrice[index].price" :placeholder="'请输入'+item.label"/>
        </el-form-item>
        <el-form-item label="总价" prop="patentTotalPrice">
          <el-input v-model="form.patentTotalPrice" placeholder="请输入总价"/>
        </el-form-item>
        <el-form-item label="详情">
          <multiple-upload max-count="6" list-type="picture-card" v-model="form.productDetailImgList"/>
          <!--          <editor v-model="form.productContent" :min-height="192"/>-->
          <!--          <el-input v-model="form.productContent" type="textarea" placeholder="请输介绍内容"/>-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct} from "@/api/sbxm/product/product";
import Editor from '@/components/Editor';
import UploadImage from '@/components/UploadImage';
import MultipleUpload from "../../../components/UploadImage/multpleUpload";

export default {
  name: "Product",
  components: {
    MultipleUpload,
    Editor,
    UploadImage,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品对象表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 服务类型字典
      productTypeOptions: [],
      // 服务类型字典
      registerCompanyMoneyOptions: [],
      // 国家/地区
      yjlmGuojiaOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productTitle: null,
        productType: "2"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getDicts("sbxmProductCode").then(response => {
      this.productTypeOptions = response.data;
    });
    this.getDicts("registerCompanyMoneyCode").then(response => {
      this.registerCompanyMoneyOptions = response.data;
    });

  },
  methods: {
    /** 查询商品对象列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getDicts("guojiaCode").then(response => {
        this.yjlmGuojiaOptions = response.data;
      });
    },
    // 服务类型字典翻译
    productTypeFormat(row, column) {
      return this.selectDictLabel(this.productTypeOptions, row.productType);
    },

    // 国家
    productGuojiaFormat(row, column) {
      return this.selectDictLabel(this.yjlmGuojiaOptions, row.productGuojia);
    },

    otherPriceInfoFormat(row, column) {
      var result = JSON.parse(row.otherPriceInfo)
      var info = "";
      result.forEach(obj => {
        info += obj.label + '：' + obj.price + '美元；';
      })
      return info;
    },

    // 国家/地区字典翻译
    yjlmGuojiaFormat(row, column) {
      return this.selectDictLabel(this.yjlmGuojiaOptions, row.yjlmGuojia);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        beginTime: null,
        createBy: null,
        createTime: null,
        endTime: null,
        remark: null,
        searchValue: null,
        updateBy: null,
        updateTime: null,
        patentPrice: null,
        otherPrice: [],
        patentTotalPrice: null,
        productContent: null,
        productImg: null,
        productImgList: [],
        productImgpc: null,
        productImgpcList: [],
        productDetailImg: null,
        productDetailImgList: [],
        productTitle: null,
        productType: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.registerCompanyMoneyOptions.forEach(obj => {
        this.form.otherPrice.push({label: obj.dictLabel})
      })
      this.open = true;
      this.title = "添加商品对象";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        console.log(this.form)
        if (this.form.productImgList == null) {
          this.form.productImgList = [];
        }
        if (this.form.productImgpcList == null) {
          this.form.productImgpcList = [];
        }
        if (this.form.productDetailImgList == null) {
          this.form.productDetailImgList = [];
        }
        this.open = true;
        this.title = "修改商品对象";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productImg = null;
          console.log(this.form)
          this.form.productImgList.forEach(obj => {
            if (this.form.productImg == null) {
              this.form.productImg = obj.path;
            } else {
              this.form.productImg += "," + obj.path;
            }
          })
          this.form.productImgpc = null;
          this.form.productImgpcList.forEach(obj => {
            if (this.form.productImgpc == null) {
              this.form.productImgpc = obj.path;
            } else {
              this.form.productImgpc += "," + obj.path;
            }
          })
          this.form.productDetailImg = null;
          this.form.productDetailImgList.forEach(obj => {
            if (this.form.productDetailImg == null) {
              this.form.productDetailImg = obj.path;
            } else {
              this.form.productDetailImg += "," + obj.path;
            }
          })
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addProduct(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品对象数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportProduct(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () {
      });
    }
  }
};
</script>
